/**
 * Created with IntelliJ IDEA.
 * Description:  反转链表(牛客网)
 * User: 86183
 * Date: 2023-04-04
 * Time: 9:18
 */

 class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class test11 {
//     头插法
//    public ListNode ReverseList(ListNode head){
//        if(head == null){
//            return null;
//        }
//        if(head.next == null){
//            return head;
//        }
//        ListNode cur = head.next;
//        head.next = null;
//        while (cur != null){
//            ListNode curNext = cur.next;
//            cur.next = head;
//            head = cur;
//            cur = curNext;
//        }
//        return head;
//    }
//
    //
     public ListNode ReverseList(ListNode head){
        ListNode head1 = new ListNode(-1);
        head1.next = head;
        ListNode pre = head1;
        ListNode cur = head;
        if(cur == null){
            return null;
        }
        while (cur.next != null){
            ListNode temp = cur.next;
            cur.next = temp.next;
            temp.next = pre.next;
            pre.next = temp;
        }
         return head1.next;
     }
}
